package com.wzdigit.wms.wms.service.wms2tms;

import com.alibaba.fastjson.JSON;
import com.wzdigit.wms.basic.domain.mid.IfPostback;
import com.wzdigit.wms.basic.enums.writeback.BackResultEnum;
import com.wzdigit.wms.basic.enums.writeback.TopicEnum;
import com.wzdigit.wms.wms.domain.dto.TmsResponse;
import com.wzdigit.wms.wms.http.TmsHttpClient;
import com.wzdigit.wms.wms.service.mid.IfPostbackService;
import com.wzdigit.wms.wms.strategy.webservice.iservice.ISendWebService;
import com.wzdigit.wms.wms.strategy.webservice.type.SendWebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * TMS出库交接单回传
 * Created by zyf on 2021/9/17 10:18
 */
@Slf4j
@Service
@SendWebService(value = TopicEnum.TOPIC_TMS_TAKE_OVER, name = "TMS出库交接单")
public class TmsBackTakeOverService  implements ISendWebService {
    @Autowired
    private TmsHttpClient tmsHttpClient;
    @Autowired
    IfPostbackService ifPostbackService;

    @Value(value = "${tms.web.service.header}")
    private String header;
    @Value(value = "${tms.web.service.dispatchTakeOver}")
    private String dispatchTakeOver;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Object send(Object object){
        IfPostback postback = new IfPostback();

        //构造发送的数据
        IfPostback ifPostback = (IfPostback) object;
        String postbackContent = ifPostback.getPostbackContent();
        log.info("---------TMS出库交接单回传---------");
        log.info(postbackContent);
        boolean flag = false;
        String returnJson = new String();//成功信息
        String error  = new String();
        try {
            TmsResponse response = tmsHttpClient.sendHttpRequest(header + dispatchTakeOver, postbackContent);
            if (response.isSuccess()){
                //调成功
                returnJson = response.getMsg();
            }else {
                //调失败
                log.error("TMS出库交接单接口异常");
                error=response.getMsg();
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("TMS出库交接单接口异常");
            error = e.getMessage();
            //发生异常，更新标识
            flag = true;
        }finally {
            if (flag) {
                //调失败
                //调接口异常，解析异常等
                postback.setIfPostbackId(ifPostback.getIfPostbackId());
                postback.setPostbackTime(new Date());
                postback.setPostbackResult(BackResultEnum.SYSTEM_ERROR_STATE.getCode());
                postback.setReturnMessage(error);

                Integer row = ifPostbackService.updateOne(postback);
                log.info("成功更新{}条记录", row);
            } else {
                //调成功
                //正常返回报文
                postback.setIfPostbackId(ifPostback.getIfPostbackId());
                postback.setPostbackTime(new Date());
                postback.setReturnMessage(returnJson);
                postback.setPostbackResult(BackResultEnum.BUSINESS_SUCCESS_STATE.getCode());

                Integer row = ifPostbackService.updateOne(postback);
                log.info("成功更新{}条记录", row);
            }
        }
        return postback;
    }

}
